iT邦幫忙

2024 iThome 鐵人賽

DAY 11
0

每天的專案會同步到 GitLab 上,可以前往 GitLab 查看。
有興趣的朋友歡迎留言 or 來信討論,我的信箱是 nickchen1998@gmail.com

今天要介紹的套件名字叫做 DataToWord,這個套件主要是協助我們將資料型態的檔案內容轉換成文章,提高我們在進行 RAG 的時候的搜索效果,目前這個套件可以協助我們處理 CSV 以及 JSON 格式的檔案。

套件網址:https://pypi.org/project/datatoword/0.7.0/

什麼是資料型態的檔案?

簡單說明一下什麼是資料型態的檔案,比方說常見的 csv、json 等等,是用來做資料傳輸、交換的,可以看一下下方的格式:

  • json
{
    "company": {
        "name": "Tech Innovators Inc.",
        "founded": 2010,
        "employees": [
            {
                "name": "Alice Smith",
                "age": 30,
                "position": "Software Engineer",
                "skills": ["Python", "Django", "Machine Learning"],
                "projects": [
                    {"name": "AI Chatbot", "duration_months": 12, "team_size": 5},
                    {"name": "E-commerce Platform", "duration_months": 8, "team_size": 3}
                ]
            },
            {
                "name": "Bob Johnson",
                "age": 35,
                "position": "Data Scientist",
                "skills": ["Python", "Pandas", "Deep Learning"],
                "projects": [
                    {"name": "Data Analysis Pipeline", "duration_months": 6, "team_size": 4},
                    {"name": "Recommendation System", "duration_months": 10, "team_size": 7}
                ]
            }
        ]
    }
}
  • csv
name,age,position,skills,project_name,project_duration_months,project_team_size
            Alice Smith,30,Software Engineer,"Python;Django;Machine Learning","AI Chatbot",12,5
            Alice Smith,30,Software Engineer,"Python;Django;Machine Learning","E-commerce Platform",8,3
            Bob Johnson,35,Data Scientist,"Python;Pandas;Deep Learning","Data Analysis Pipeline",6,4
            Bob Johnson,35,Data Scientist,"Python;Pandas;Deep Learning","Recommendation System",10,7

而我們在進行 RAG 的時候,是將文本的內容轉換為向量,然後存進資料庫當中進行查詢,而在搜尋向量的時候,理論上會去搜尋與你的語句最相近的段落,因此可想而知,如果直接把上面的資料轉換成向量,然後使用一班正常問答的話,會沒有辦法正確找到相對應的資料。

該怎麼使用這個套件?

這邊想到的辦法是將所有的資料,重新轉換成一段一段可以讀的文本,然後再寫入到向量資料庫當中進行查詢,讓我們看一下 DataToWord 這個套件的用法:

  • create_documents

這個套件是使用 LangChain 套件來協助串接 OpenAI 的模型,預設是使用 gpt-4o,我們可以透過直接把 binary 型態的檔案傳遞給 create_documents 這個方法,來取得轉換後的文本內容。

from datatoword import DataToWord


with open('data.csv', 'rb') as file:
    file_binary_content = file.read()

data_to_word = DataToWord()
data_to_word.create_documents(
    file_name='data.csv',
    file_description='這是一個測試的檔案',
    file_binary_content=file_binary_content
)
  • create_content

當然,如果你只想單純取得轉換後的文本內容的話,也可以呼叫 create_content 這個方法,裡面會直接協助你轉換成 List[str],這個資料型態,讓你直接做取用

from datatoword import DataToWord


with open('data.csv', 'rb') as file:
    file_binary_content = file.read()

data_to_word = DataToWord()
data_to_word.create_content(
    file_name='data.csv',
    file_description='這是一個測試的檔案',
    file_binary_content=file_binary_content
)

內容預告

以上就是今天的內容,明天我們要來介紹 chunk 的大小和搜尋結果的關聯。


上一篇
Day 10 - RAG 程式碼實作
下一篇
Day 12 - DeepEval 評估測試套件介紹 - 忠誠度 & 關聯性
系列文
初探 Langchain 與 LLM:打造簡易問診機器人13
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言